﻿/// <reference path="../common/common.js" />
!function () {
    var $ = fly;
    fly.box.fixParam = '$with-org=1'
    $.box.isWithOrg = true;

    fly.box.Index = $.Class({
        constructor: function () {
            this.createMembers();
            this.bindEvents();
            this.getLoginInfo();
        },
        loginInfo: { folderMenus: {} },
        getLoginInfo: function () {
            $.base.getUserContext(function () {
                index.init(window.userContext);
            });
        },
        init: function (data) {
            this.loginInfo = data
            this.showSpaceSize()
            this.reBuildFolderMenus()

            $('#user-name').html('{0}-<a href=index.htm title="转到我的个人网盘">{1}</a>'.format(data.org.name, data.user.name + '[个人网盘]'));

            if (data.logo) {
                $('#user-ico').attr('src', data.logo);
            }

            if (data.isFirst)
                $.loadScript('js/first.js')
        },
        createMembers: function () {
            this.searchBox = new $.ui.EmptyBox("#search");
            this.searchForm = $('#search-form');
            this.contentFrame = $('#content-frame');
            this.folderMenus = $('#folder-menus')
        },
        bindEvents: function () {
            var me = this;
            this.searchForm.submit(function () {
                search(me.searchBox.value());
                return false;
            });
        },
        logout: function () {
            $.post($.box.ajaxUrl('sys', 'logout'), function (result) {
                if (result.success) {
                    window.open(result.data || 'login.htm', '_top');
                }
                else {
                    $.alert(result.msg)
                }
            })
        },
        showSpaceSize: function () {
            var space = this.loginInfo.space;
            var scale = space.used * 100 / space.size;
            if (scale > 0 && scale < 1)
                scale = 1
            $('#space-size-pro div').width(scale + '%')
            $('#space-size').html('{0} / {1}'.format($.sizeFormat(space.used), $.sizeFormat(space.size)))
            $('#space-size').parent().attr('title', '空间总大小{0}，已使用{1}。'.format($.sizeFormat(space.size), $.sizeFormat(space.used)))
        },
        reBuildFolderMenus: function () {
            var menus = this.loginInfo.folderMenus = this.loginInfo.folderMenus || []
            var ms = function (m) {
                menus[m.id] = m;
            };
            menus.org && menus.org.each(ms);
            var format = '{@for o as menu}<li><a href="file/manage.htm?{0}&path={menu.id}" target=f >{menu.name}</a></li>{for@}'
            if (menus.org)
                this.folderMenus.html(format.format('$with-org=1', menus.org));
        }
    });

    window.index = window.page = new fly.box.Index();
    window.search = function (key) {
        try {
            index.contentFrame[0].contentWindow.search(key);
        } catch (e) { }
    }
} ()

